package com.zzyl.nursingPlan.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursingPlan.domain.NursingPlan;
import com.zzyl.nursingPlan.domain.dto.NursingPlanDto;
import com.zzyl.nursingPlan.mapper.NursingPlanMapper;
import com.zzyl.nursingPlan.mapper.NursingProjectPlanMapper;
import com.zzyl.nursingPlan.service.INursingPlanService;
import com.zzyl.nursingProject.domain.dto.NursingProjectPlanDto;
import com.zzyl.nursingProject.domain.vo.NursingPlanVo;
import com.zzyl.nursingProject.domain.vo.NursingProjectPlanVo;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 护理计划Service业务层处理
 *
 * @author yezude
 * @date 2025-01-01
 */
@Transactional
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper,NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 新增护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    public int insertNursingPlan(NursingPlanDto dto) {
        //保存护理计划
        //属性拷贝
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);
        nursingPlan.setCreateTime(DateUtils.getNowDate());


        List<NursingProjectPlanDto> projectPlans = dto.getProjectPlans();
        //批量保存护理项目计划关系

        save(nursingPlan);

        for (int i = 0; i < projectPlans.size(); i++) {
            if(projectPlans.get(i).getProjectId().isEmpty()||nursingPlan.getId()==null){
                projectPlans.remove(i);
                i--;
            }
        }
        if(projectPlans.size()>0){
            Long id = nursingPlan.getId();
            return nursingProjectPlanMapper.batchInsert(dto.getProjectPlans(), nursingPlan.getId());
        }else {
            return 1;
        }

    }

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
        //查询护理计划
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);

        //根据护理计划ID查询护理项目的关系
        List<NursingProjectPlanVo> list = nursingProjectPlanMapper.selectByPlanId(id);
        nursingPlanVo.setProjectPlans(list);

        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Override
    public int insertNursingPlan(NursingPlan nursingPlan) {

        nursingPlan.setUpdateTime(new Date());
        return save(nursingPlan)?1:0;
    }

    /**
     * 修改护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */

    @Override
    public int updateNursingPlan(NursingPlanDto dto) {
        try {
            //属性拷贝
            NursingPlan nursingPlan = new NursingPlan();
            BeanUtils.copyProperties(dto, nursingPlan);


            //删除护理计划与护理项目的关系
            nursingProjectPlanMapper.deleteByPlanId(dto.getId());


            List<NursingProjectPlanDto> projectPlans = dto.getProjectPlans();

            //判断dto中的项目列表为空，如果不为空，则先删除护理计划与护理项目的关系，然后重新批量添加
            if (dto.getProjectPlans() != null && dto.getProjectPlans().size() > 0) {
                //批量添加护理计划与护理项目的关系
                for (int i = 0; i < projectPlans.size(); i++) {
                    if(projectPlans.get(i).getProjectId().isEmpty()||nursingPlan.getId()==null){
                        projectPlans.remove(i);
                        i--;
                    }
                }
                nursingProjectPlanMapper.batchInsert(projectPlans, dto.getId());
            }


            //别管项目列表是否为空，都要修改护理计划
            return updateById(nursingPlan)?1:0;
        } catch (BeansException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id) {
        try {
            //删除关系
            //删除护理计划与护理项目的关系
            nursingProjectPlanMapper.deleteByPlanId(id);
            return removeById(id)?1:0;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
